<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>申领单回款</title>
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--{include file='comm/comm_pic.tpl'}-->
    <style type="text/css">
        .items2 ul{ }
        .items2 ul li{margin-bottom: 10px;border: 1px solid #ffffff;height: 30px;line-height: 30px;padding: 0 5px;}
        .items2 ul li.selected {background-color: #E8F1FF;border: 1px solid #B2D1FF;box-shadow: 2px 2px 0 #E5E5E5;font-weight: bold;}
    </style>
</head>

<body>

<div class="container">

    <div style="width:98%; margin: 0 auto;">
    
        <div class="title">
            <h2 class="tac">
                <!--{$card_apply_array.apply_id}--> - 申领单回款
            </h2>
        </div>

        <div class="panel panel-default">
            <h2 class="panel-heading">申领单信息</h2>
            <div class="items2">
                <table class="form-inline">
                    <tbody>
                    <tr style="height: 40px;">
                        <td class="tal">客户名称：<input type="text" class="form-control w150" name="customer_name" value="<!--{$card_apply_array.customer_name}-->" readonly="readonly"></td>
                        <td class="tal">申请日期：<input type="text" class="form-control w150" name="apply_date" value="<!--{$card_apply_array.apply_date}-->" readonly="readonly"></td>
                    </tr>
                    <tr style="height: 40px;">
                        <td class="tal">联 系 人：<input type="text" class="form-control w150" name="linkman" value="<!--{$card_apply_array.linkman}-->" readonly="readonly"></td>
                        <td class="tal">联系方式：<input type="text" class="form-control w150" name="contact_way" value="<!--{$card_apply_array.contact_way}-->" readonly="readonly"></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>


        <!--现金卡信息-->

        <div class=" panel panel-default">
            <h2 class="panel-heading">
                现金卡信息
            </h2>
            <table class="table">
                <thead>
                <tr>

                </tr>
                </thead>
                <thead>
                <tr>
                    <th>面值</th>
                    <th>规格</th>
                    <th>数量</th>
                    <th>已发数量</th>
                    <th>折扣率</th>
                    <th>折后价</th>
                    <th>代金卡类型</th>
                    <th>备注</th>
                </tr>
                </thead>

                <tbody>
                <!--{foreach from=$card_detail_array item=card_detail}-->
                <tr>
                    <td><!--{$card_detail.face_value}--></td>
                    <td><!--{$card_detail.card_spec}--></td>
                    <td><!--{$card_detail.card_num}--></td>
                    <td><!--{$card_detail.send_card_num}--></td>
                    <td><!--{$card_detail.card_discount}--></td>
                    <td><!--{$card_detail.card_total_amount}--></td>
                    <td><!--{$card_detail.card_type}--></td>
                    <td><!--{$card_detail.remark}--></td>
                </tr>
                <!--{/foreach}-->
                </tbody>

            </table>
        </div>

        <form id="item_form" onsubmit="return false;" class="form-horizontal">
            <input type="hidden" name="returned_id" id="returned_id" value="0">
            <div class=" panel panel-default">
                <h2 class="panel-heading">
                    回款信息
                </h2>
                <div class="items2">
                    <table class="form-inline">
                        <tbody>
                            <tr>
                                <td>
                                    <span style="float: left">
                                        可回款金额：
                                        <input type="text" class="form-control w150" id="returnedAmount" readonly="readonly" name="returnedAmount" value="<!--{$card_apply_array.total_amount-$card_apply_array.returned_amount|string_format:'%.2f'}-->"/>&ensp;&ensp;&ensp;
                                    </span>
                                    <span style="float: left">
                                         回款方式：
                                         <select class="form-control w150" id="returned_type_id" name="returned_type_id">
                                             <option value="">请选择</option>
                                             <!--{foreach from=$returned_array item=returned_number}-->
                                             <option value="<!--{$returned_number.itemID}-->"><!--{$returned_number.itemName}--></option>
                                             <!--{/foreach}-->
                                         </select>&ensp;&ensp;&ensp;
                                    </span>

                                    <span style="float: left">
                                        回款金额：
                                        <input type="text" class="form-control w150" id="returned_amount" placeholder="请输入回款金额" name="returned_amount" value=""/>&ensp;&ensp;&ensp;
                                    </span>

                                    <span style="float: left" id='backInput'></span>
                                </td>
                            </tr>
                            <tr>
                                <td><br></td>
                            </tr>
                            <tr >
                                <td>
                                     <span>
                                        &ensp;&ensp;&ensp;&ensp;&ensp;备 注：
                                        <textarea id="remark" class="form-control w300" rows="3" name="remark"  placeholder="请输入备注"></textarea>
                                    </span>
                                     <span style="padding: 0 60px;">
                                        <button type="button" class="btn btn-success" onclick="cardApplyList.returned();">新增回款</button>
                                    </span>
                                </td>
                            </tr>

                        </tbody>
                    </table>
                </div>
            </div>
        </form>


        <div class="panel panel-default">
            <div class="panel-heading">回款列表</div>
            <table class="table">
                <thead>
                <tr>
                    <th width="200">回款方式</th>
                    <th width="300">回款金额</th>
                    <th width="300">实际回款日期</th>
                    <th width="300">确认回款时间</th>
                    <th width="300">备注</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody class="form-inline" id="invoice_list">
                <!--{if $returned_money}-->
                <!--{foreach from=$returned_money item=returned}-->
                <tr>
                    <td><!--{$returned.returned_type_name}--></td>
                    <td><!--{$returned.returned_amount|string_format:'%.2f'}--></td>
                    <td><!--{$returned.backTime}--></td>
                    <td><!--{$returned.confirm_time}--></td>
                    <td><!--{$returned.remark}--></td>
                    <td id="returned_id_line_<!--{$returned.returned_id}-->">
                        <!--{if $returned.is_confirm == 0}-->
                            <!--{if $returned_permission}-->
                                <input type="button" class="btn btn-primary btn-sm" value="确认回款" onclick="cardApplyList.confirmIeturned(<!--{$returned.returned_id}-->);" />
                                <input type="button" class="btn btn-primary btn-sm" value="删除" onclick="cardApplyList.del(<!--{$returned.returned_id}-->);" />
                            <!--{/if}-->
                        <!--{/if}-->
                    </td>
                </tr>
                <!--{/foreach}-->
                <!--{/if}-->
                </tbody>
            </table>
        </div>
    </div>

</div>

<script type="text/javascript">

    var cardApplyList = {

        returned_id:'',
        apply_id: <!--{$card_apply_array.apply_id}-->,

        returned: function(){

            this.dt = E.getFormValues('item_form');
            var error_msg = '';

            //实际回款日期
            if (E.isEmpty(this.dt.backTime) && $("#backTime").length >0){
                error_msg += '实际回款日期不能为空<br/>';
            }

            if (E.isEmpty(this.dt.returned_amount)){
                error_msg += '回款金额不能为空<br/>';

            } else if(!E.isMoney(this.dt.returned_amount)){
                error_msg += '回款金额错误<br/>';
            }

            if(this.dt.returned_type_id == '')
                error_msg += '请选择回款方式<br/>';

            if(parseFloat(this.dt.returned_amount) > parseFloat(this.dt.returnedAmount)){
                error_msg += '回款金额不能大于可回款金额<br/>';
            }

            if (this.dt.returned_type_id != 0) {
                this.dt.returned_type_name = $('#returned_type_id').find('option:selected').text();
            } else {
                this.dt.returned_type_name = '';
            }

            if (error_msg != '') {
                E.alert(error_msg);
            }else{
                E.confirm('您确认新增申领单回款吗？', 'cardApplyList.execute');
            }

        },

        execute: function() {

            E.loadding.open('正在保存，请稍候...');

            E.ajax_post({
                url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=11',
                data:{
                    apply_id: this.apply_id,
                    returned_id: this.dt
                },
                call: 'cardApplyList.result'
            });

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {


//                var returnedAmount = parseFloat( $("#returnedAmount").val());
//                returnedAmount = returnedAmount - parseFloat( this.dt.returned_amount);
//                E.opener("cardApplyList.load");
//                E.alert(o.message, 2);
//                $("#returnedAmount").val( returnedAmount.toFixed(2) );
//                $("#returned_type_id").val('');
//                $("#returned_amount").val('');
//                $("#remark").val('');
//
//                var html = '<tr>';
//                html += '<td>' + this.dt.returned_type_name + '</td>';
//                html += '<td>' + parseFloat(this.dt.returned_amount).toFixed(2) + '</td>';
//                html += '<td>' + this.dt.backTime+ '</td>';
//                html += '<td>' + this.dt.confirm_time+ '</td>';
//                html += '<td>' + this.dt.remark + '</td>';
//                html += '<td id="returned_id_line_' + o.data.returned_id + '">';
//                html += '&nbsp;<input type="button" class="btn btn-primary btn-sm" value="确认回款" onclick="cardApplyList.confirmIeturned(' + o.data.returned_id + ');" />';
//                html += '&nbsp;<input type="button" class="btn btn-primary btn-sm" value="删除" onclick="cardApplyList.del(' + o.data.returned_id + ');" />';
//
//                html += '</td>';
//                html += '</tr>';
//
//                $('#invoice_list').append(html);
                history.go(0);

            } else {
                E.alert(o.message);
            }

        },

        confirmIeturned: function( returned_id ) {

            if (!returned_id) {
                returned_id = 0;
            } else {

                E.confirm('您确认回款吗？', function() {

                    E.loadding.open('正在保存，请稍候...');
                    E.ajax_get({
                        url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=12',
                        data: {
                            returned_id: returned_id
                        },
                        call: function( o ) {

                            E.loadding.close();

                            if (o.code == 200) {

                                E.alert('确认回款成功', 2);

                                $("#returned_id_line_" + returned_id).hide();

                            } else {
                                E.alert(o.message);
                            }

                        }
                    });

                });

            }

        },
        del:function( returned_id ) {

            if (!returned_id) {
                returned_id = 0;
            } else {

                E.confirm('您确认删除？', function() {

                    E.loadding.open('正在删除，请稍候...');
                    E.ajax_get({
                        url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=14',
                        data: {
                            returned_id: returned_id
                        },
                        call: function( o ) {

                            E.loadding.close();

                            if (o.code == 200) {

                                E.alert('删除成功', 2);
                                history.go(0);



                            } else {
                                E.alert(o.message);
                            }

                        }
                    });

                });

            }

        }

    };

    $(function(){

        //根据不同的回款方式来加载不同的回款日期
        $("#returned_type_id").change(function(){
                //回款方式ID
                var backWay=$(this).val();
                //回款方式文本
               var backtext = $("#returned_type_id  option:selected").text();
                var backHtml="";
                if(backWay!=308 && backWay!=310){
                    var baht=backtext+"回款日期：<input type='text' name='backTime' id='backTime' value=''  class='form-control w150' readonly='readonly'>";
                    $("#backInput").html(baht);
                }else{
                    $("#backInput").html('');
                }
            $('#backTime').datepicker();
        });



    });

</script>



</body>
</html>
